<template>
  <view class="demo-container">
    <!-- 基础用法 -->
    <view class="demo-block">
      <view class="demo-block__title">基础用法</view>
      <view class="demo-block__content">
        <fu-form 
          :model="form1" 
          :rules="rules1"
          label-width="200rpx"
          ref="form1"
        >
          <fu-form-item label="用户名" prop="username">
            <fu-input v-model="form1.username" placeholder="请输入用户名" />
          </fu-form-item>
          <fu-form-item label="密码" prop="password">
            <fu-input 
              v-model="form1.password" 
              type="password"
              placeholder="请输入密码" 
            />
          </fu-form-item>
          <fu-form-item>
            <fu-button type="primary" @click="submitForm1">提交</fu-button>
            <fu-button @click="resetForm1">重置</fu-button>
          </fu-form-item>
        </fu-form>
      </view>
    </view>
    
    <!-- 表单校验 -->
    <view class="demo-block">
      <view class="demo-block__title">表单校验</view>
      <view class="demo-block__content">
        <fu-form 
          :model="form2" 
          :rules="rules2"
          label-position="top"
          ref="form2"
        >
          <fu-form-item label="姓名" prop="name">
            <fu-input v-model="form2.name" placeholder="请输入姓名" />
          </fu-form-item>
          <fu-form-item label="年龄" prop="age">
            <fu-number-box v-model="form2.age" :min="0" :max="120" />
          </fu-form-item>
          <fu-form-item label="性别" prop="gender">
            <fu-radio-group v-model="form2.gender">
              <fu-radio :label="1">男</fu-radio>
              <fu-radio :label="2">女</fu-radio>
            </fu-radio-group>
          </fu-form-item>
          <fu-form-item label="爱好" prop="hobbies">
            <fu-checkbox-group v-model="form2.hobbies">
              <fu-checkbox label="reading">阅读</fu-checkbox>
              <fu-checkbox label="music">音乐</fu-checkbox>
              <fu-checkbox label="sports">运动</fu-checkbox>
            </fu-checkbox-group>
          </fu-form-item>
          <fu-form-item>
            <fu-button type="primary" @click="submitForm2">提交</fu-button>
          </fu-form-item>
        </fu-form>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form1: {
        username: '',
        password: ''
      },
      rules1: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      form2: {
        name: '',
        age: 18,
        gender: 1,
        hobbies: []
      },
      rules2: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { type: 'number', message: '年龄必须为数字', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        hobbies: [
          { type: 'array', required: true, message: '请至少选择一个爱好', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm1() {
      this.$refs.form1.validate(valid => {
        if (valid) {
          console.log('表单1验证通过')
        } else {
          console.log('表单1验证失败')
        }
      })
    },
    resetForm1() {
      this.$refs.form1.resetFields()
    },
    submitForm2() {
      this.$refs.form2.validate(valid => {
        if (valid) {
          console.log('表单2验证通过')
        } else {
          console.log('表单2验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-container {
  padding: 20rpx;
}

.demo-block {
  margin-bottom: 30rpx;
  
  &__title {
    margin-bottom: 20rpx;
    font-size: var(--fu-font-size-lg);
    color: var(--fu-text-color);
  }
  
  &__content {
    background-color: #fff;
    padding: 30rpx;
    border-radius: var(--fu-radius-base);
    
    .fu-button {
      margin-right: 20rpx;
      
      &:last-child {
        margin-right: 0;
      }
    }
  }
}
</style> 